<?php
namespace Admin\Controller;

use Common\Controller\AdminBaseController;
use Lib\MyTree;

class ArticleController extends AdminBaseController
{
    public function index()
    {
        $curr = I('get.p/d',0);
        // 获取前端传来的查询词
        $word = I('post.word','');
        $totalNum = 5;
        $article = M('article');
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $sqlWhere = empty($word) ? '1=1' : 'fd_article.title like "%'.$word.'%"';
        $list = $article->field('fd_article.id,fd_article.title,fd_article.add_time,c.category')->join('left join fd_category c on fd_article.cate_id=c.id')->where($sqlWhere)->page($curr.','.$totalNum)->select();
        // 赋值数据集
        $this->assign('list',$list);
        // 查询满足要求的总记录数
        $count = $article->where($sqlWhere)->count();
        // 实例化分页类 传入总记录数和每页显示的记录数
        $Page  = new \Think\Page($count,$totalNum);
        // 分页显示输出
        $show  = $Page->show();
        $this->assign('word',$word);
        // 赋值分页输出
        $this->assign('page',$show);
        $this->assign('title','FD商城文章表');
        // 输出模板
        $this->display('list');
    }
    public function add()
    {
        if (IS_AJAX) {
            //获取标题
            $title= I('post.title','');
            if(empty($title)){
                $this->ajaxReturn(['state'=>1,'msg'=>'文章标题不能为空！']);
            }
            //获取内容
            $content = I('post.content','');
            $content = strip_tags(htmlspecialchars_decode($content));
            if(empty($content)){
                $this->ajaxReturn(['state'=>1,'msg'=>'文章内容不能为空！']);
            }
            //获取前端隐藏ID
            $cate_id = I('post.pc',1);
            $article_info = M('article')->add(['title'=>$title,'content'=>$content,'add_time'=>time(),'cate_id'=>$cate_id]);
            //判断SQL是否插入成功
            if($article_info){
                $this->ajaxReturn(['state' => 0,'msg'=>'添加文章成功']);
            }
            $this->ajaxReturn(['state'=>1,'msg'=>'添加文章失败！']);

        }else{
            //获取分类表中的分类信息
            $category_info = M('category')->field('id,category,pid')->order('pid asc')->select();
            //实例化一个无限极分类对象
            $apple = new MyTree();
            //获取分类信息的结果集
            $tree = $apple->my_tree($category_info);
            //传输数据到前端输出
            $this->assign('tree', $tree);
            //加载前端页面
            $this->display();
        }
    }
    public function del()
    {
        if(IS_POST){
            //获取文章ID
            $id = I('post.id/d',0);
            //判断ID是否合法
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            //执行SQL操作，判断是否成功
            if(M('Article')->delete($id) == false){
                $this->ajaxReturn(['state'=>1,'msg'=>'删除失败！']);
            }
            $this->ajaxReturn(['state'=>0,'msg'=>'删除成功！']);
        }else{
            $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
        }
    }
    public function edit()
    {
        if(IS_POST){
            //获取修改过后的文章ID
            $id = I('post.id',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'文章ID不能为空！']);
            }
            //获取修改的文章标题
            $title= I('post.title','');
            if(empty($title)){
                $this->ajaxReturn(['state'=>1,'msg'=>'文章标题不能为空！']);
            }
            //获取修改的文章内容
            $content = I('post.content','');
            $content = strip_tags(htmlspecialchars_decode($content));
            if(empty($content)){
                $this->ajaxReturn(['state'=>1,'msg'=>'文章内容不能为空！']);
            }
            //获取PID
            $cate_id = I('post.pc',1);
            //执行SQL添加操作
            $article_info = M('article')->where(['id'=>$id])->setField(['title'=>$title,'content'=>$content,'cate_id'=>$cate_id]);
            //判断是否添加成功
            if($article_info){
                $this->ajaxReturn(['state' => 0,'msg'=>'修改文章成功']);
            }
            $this->ajaxReturn(['state'=>1,'msg'=>'修改文章失败！']);
        }else{
            //获取文章分类表中的分类信息
            $category_info = M('category')->field('id,category,pid')->order('pid asc')->select();
            //实例化无限级分类的类
            $apple = new MyTree();
            //获取分类结果集
            $tree = $apple->my_tree($category_info);
            //获取要修改的文章ID
            $id = I('get.id',0);
            //判断ID是否存在
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'文章ID不能为空！']);
            }
            //执行SQL操作，查出一条数据
            $apple = M('Article')->field('id,title,content,cate_id')->where(array('id'=>$id))->find();
            //文章内容数据，传给前端
            $this->assign('apple_info',$apple);
            //文章分类数据，传给前端
            $this->assign('tree', $tree);
            //加载修改页面
            $this->display();
        }
    }
}